//
//  UrlHelper.m
//  FengHuangFM
//
//  Created by lanou3g on 15/12/15.
//  Copyright © 2015年 lanou3g. All rights reserved.
//

#import "UrlHelper.h"
static UrlHelper *helper = nil;
@implementation UrlHelper
/*---------------------------------------
 单例文件
 --------------------------------------- */
+ (instancetype)shareHelper {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        helper = [[UrlHelper alloc] init];
    });
    return helper;
}

/*---------------------------------------
 主页数据模型请求方法
 --------------------------------------- */
- (void)getDictionaryWithUrl:(NSString *)urlStr urlBody:(NSString *)body success:(void(^)(id))success fail:(void(^)(NSError *))fail
{
    //判断url是否为空
    if (!urlStr) {
        return;
    }
    //开辟子线程
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        //请求数据
       NSMutableArray *array = [NSMutableArray array];
        NSURL *url = [NSURL URLWithString:urlStr];
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
        [request setHTTPMethod:@"post"];
        NSData *data = [body dataUsingEncoding:NSUTF8StringEncoding];
        [request setHTTPBody:data];
        NSURLSession *session = [NSURLSession sharedSession];
        NSURLSessionTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
            NSDictionary *dataDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
            for (NSDictionary *dic in dataDic[@"data"]) {
                ListCategoryModel *caModel = [[ListCategoryModel alloc] init];
                caModel.ReourceArray = [NSMutableArray array];
                caModel.cardTitle = dic[@"cardTitle"];
                if ([dic[@"cardTitle"] isEqualToString:@"专题"]) {
                    caModel.themeArray = [NSMutableArray array];
                    for (NSDictionary *dict in dic[@"listCategory"][@"listSpecial"]) {
                        ThemeModel *themeModel = [[ThemeModel alloc] init];
                        [themeModel setValuesForKeysWithDictionary:dict];
                        [caModel.themeArray addObject:themeModel];
                    }
                } else {
                    if (dic[@"listCategory"] == nil) {
                        for (NSDictionary *sonDic in dic[@"listReource"]) {
                            [self dictionaryAnalysis:sonDic model:caModel];
                        }
                    } else {
                        for (NSDictionary *sonDic in dic[@"listCategory"][@"listReource"]) {
                            [self dictionaryAnalysis:sonDic model:caModel];
                        }
                    }
                }
                [array addObject:caModel];
            }
            //回主线程
            dispatch_async(dispatch_get_main_queue(), ^{
                success(array);
            });
        }];
        [task resume];
    });
}

- (void)dictionaryAnalysis:(NSDictionary *)dic model:(ListCategoryModel *)caModel{
    ListReourceModel *reModel = [[ListReourceModel alloc] init];
    [reModel setValuesForKeysWithDictionary:dic];
     //NSLog(@"%@",reModel.img100_100);
    reModel.audioArray = [NSMutableArray array];
    for (NSDictionary *childDic in dic[@"audiolist"]) {
        [reModel.audioArray addObject:childDic[@"filePath"]];
    }
    if (caModel.ReourceArray) {
        [caModel.ReourceArray addObject:reModel];
    } else {
        caModel.ReourceArray = [NSMutableArray array];
        [caModel.ReourceArray addObject:reModel];
    }
   // NSLog(@"-----------------%@", reModel.bigPictureUrl);
}


@end
